package com.itheima.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mapper.UmsRoleResourceRelationMapper;
import com.itheima.pojo.UmsResource;
import com.itheima.pojo.UmsRoleMenuRelation;
import com.itheima.pojo.UmsRoleResourceRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 袁鹏
 * @date 2022-10-13-10:33
 */
@DubboService
public class UmsRoleResourceRelationApiImpl implements UmsRoleResourceRelationApi{

    @Autowired
    private UmsRoleResourceRelationMapper umsRoleResourceRelationMapper;


    /**
     * 给角色分配资源
     */
    @Override
    public void allocResource(Integer[] resourceIds, Integer roleId) {
        // 获取角色已分配资源
        LambdaQueryWrapper<UmsRoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleResourceRelation::getRoleId, roleId);
        List<UmsRoleResourceRelation> allocedResourceList = umsRoleResourceRelationMapper.selectList(queryWrapper);
        // 提取已分配资源id列表
        List<Long> allocedResourceIdList = CollUtil.getFieldValues(allocedResourceList, "resourceId", Long.class);
        // 创建要分配资源id集合
        Set<Long> willAllocResourceIdSet = Arrays.stream(resourceIds).map(Convert::toLong).collect(Collectors.toSet());
        // 做差集运算
        willAllocResourceIdSet.removeAll(allocedResourceIdList);
        // 分配
        if(willAllocResourceIdSet.size() > 0) {
            umsRoleResourceRelationMapper.allocResource(resourceIds, roleId);
        }
    }


    /**
     * 根据角色id获取关联的资源id
     */
    @Override
    public List<Long> getRoleRelatedResourceId(Integer roleId) {
        LambdaQueryWrapper<UmsRoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleResourceRelation::getRoleId, roleId);
        List<UmsRoleResourceRelation> roleResourceRelationList = umsRoleResourceRelationMapper.selectList(queryWrapper);
        return CollUtil.getFieldValues(roleResourceRelationList, "resourceId", Long.class);
    }


    /**
     * 根据资源id删除与角色的关联
     */
    @Override
    public void deleteByResourceId(Long id) {
        LambdaQueryWrapper<UmsRoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleResourceRelation::getResourceId, id);
        umsRoleResourceRelationMapper.delete(queryWrapper);
    }

    @Override
    public void deleteByResourceId(List<Long> idList) {
        if(idList.size() > 0){
            LambdaQueryWrapper<UmsRoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(UmsRoleResourceRelation::getResourceId, idList);
            umsRoleResourceRelationMapper.delete(queryWrapper);
        }
    }


    /**
     * 根据角色id删除与资源的关联
     */
    @Override
    public void deleteByRoleId(Integer[] ids) {
        if(ids.length > 0){
            LambdaQueryWrapper<UmsRoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(UmsRoleResourceRelation::getRoleId, ids);
            umsRoleResourceRelationMapper.delete(queryWrapper);
        }
    }


}
